/**
 * JSparrow - Une version numérique du jeu "Attention Pirates !" - Copyright (C) 2014 Black Pearl
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program. If
 * not, see <http://www.gnu.org/licenses/>.
 */
package App.Vue;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


@SuppressWarnings("serial")
public class Vue_Bateau extends JPanel_Principal{

	//Bordure extérieur en px
	private int BORDER = 0;
	
	//Bateau courant
	private String couleur;
	private String nbTresors;
	
	//Image redimensionnée du bateau courant
	private Image boatImage;
	
	//Image redimensionnées des éléments
	private Image coffre1;
	private Image coffre2;
	private Image coffre3;
	private Image coffre4;
	private Image coffre5;
	private Image coffre6;
	private Image canon;
	private Image pirate;
	
	//Ratio pour redimensionner les élément graphique
	private double ratio  = 1;
	
	//Tableau contenant les élément, l'itérateur et le No d'emplacemnt
	private String emplacements[]; 

	/**
	 * Créer un nouveau panel bateau
	 * @param couleur Couleur du joueur ex "0"
	 * @param pieces taleau String[6] contenant les pieces ex {"pirate","canon",coffre5",""...}
	 */
	public Vue_Bateau(String couleur, String[] pieces, String nbTresors){
		
		//Transparence du fond
		this.setBackground(TRANSPARENT);
		this.setOpaque(false);
		Rectangle screenBounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
		this.ratio = 587/(screenBounds.height*0.26);
		int SCRX =(int)(screenBounds.width*0.18); 
		int SCRY = (int)(screenBounds.height*0.26);
		this.setPreferredSize(new Dimension(SCRX,SCRY));
		this.couleur = couleur;
		this.nbTresors = nbTresors;
		initImages();
		emplacements = pieces;
	}

	/**
	 * Redéfini la manire de peindre le panel
	 */
	public void paintComponent(Graphics g){
		int i,x,y;
		Image element;
		Point p;
		super.paintComponent(g);
		g.drawImage(boatImage, BORDER, BORDER, this);
		String composant = new String("");

		for(i=0;i<6;i++){
			
			if(emplacements[i].equals("3")){
				composant = "c"+nbTresors;
			}
			else{
				composant = emplacements[i];
			}
			element = getElement(composant);

			if(element !=null){
				p = getPostion(i);
				x = p.x - (int)(element.getWidth(null)/2);
				y = p.y - (int)(element.getHeight(null)/2);
				g.drawImage(element, x, y, this);
			}

		}
	}


	/**
	 * Determine l'image à afficher suivant l'élément passé en paramètre
	 * @param type piece de l'emplacement
	 * @return Image associée
	 */
	public Image getElement(String type){
		switch(type){
		case "1" :
			return pirate;
		case "2" :
			return canon;
		case "c1" :
			return coffre1;
		case "c2" :
			return coffre2;
		case "c3" :
			return coffre3;
		case "c4" :
			return coffre4;
		case "c5" :
			return coffre5;
		case "c6" :
			return coffre6;
		default : 
			return null;
		}
	}

	/**
	 * Donne le point de l'angle supérieur gauche de l'emplacement
	 * @param emplacement numéro de l'emplacement du dés
	 * @return Point en px
	 */
	public Point getPostion(int emplacement){

		Point p = new Point(0,0);

		switch(emplacement){
		case 0 : 
			p.setLocation(BORDER + (int)(103/ratio), BORDER + (int)(163/ratio));
			break;
		case 1 : 
			p.setLocation(BORDER + (int)(193/ratio), BORDER + (int)(163/ratio));
			break;
		case 2 : 
			p.setLocation(BORDER + (int)(70/ratio), BORDER + (int)(270/ratio));
			break;
		case 3 : 
			p.setLocation(BORDER + (int)(145/ratio), BORDER + (int)(270/ratio));
			break;
		case 4 : 
			p.setLocation(BORDER + (int)(224/ratio), BORDER + (int)(270/ratio));
			break;
		case 5 : 
			p.setLocation(BORDER + (int)(148/ratio), BORDER + (int)(517/ratio));
			break;			
		}

		return p;
	}

	/**
	 * Détermine la couleur du bateau ainsi que la disposition des pieces sur celui-ci
	 * @param couleur Couleur du joueur
	 * @param pieces Tableau[6] contenant les pieces du joueur
	 */
	public void updateBateau(String couleur, String[] pieces, String nbTresors){

		emplacements = pieces;
		this.nbTresors = nbTresors;
		if(this.couleur != couleur){
			this.couleur = couleur;
			try{
				BufferedImage bufferedImg = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("img/bateaux/"+couleur+".png"));			
				boatImage = bufferedImg.getScaledInstance((int)(bufferedImg.getWidth()/ratio),(int)(bufferedImg.getHeight()/ratio), 0);
			}
			catch (IOException e) {
				e.printStackTrace();
			}
		}
		this.repaint();
	}

	/**
	 * Initialise les images et les redimensionne
	 */
	public void initImages(){
		try {
			// Resize les images en fct d'un ratio, permet le faire qu'une fois pour le panel
			BufferedImage bufferedImg = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("img/bateaux/"+couleur+".png"));			
			boatImage = bufferedImg.getScaledInstance((int)(bufferedImg.getWidth()/ratio),(int)(bufferedImg.getHeight()/ratio), 0);
			bufferedImg = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("img/coffres/coffre1.png"));
			coffre1 = bufferedImg.getScaledInstance((int)(bufferedImg.getWidth()/ratio),(int)(bufferedImg.getHeight()/ratio), 0);
			bufferedImg = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("img/coffres/coffre2.png"));
			coffre2 = bufferedImg.getScaledInstance((int)(bufferedImg.getWidth()/ratio),(int)(bufferedImg.getHeight()/ratio), 0);
			bufferedImg = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("img/coffres/coffre3.png"));
			coffre3 = bufferedImg.getScaledInstance((int)(bufferedImg.getWidth()/ratio),(int)(bufferedImg.getHeight()/ratio), 0);
			bufferedImg = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("img/coffres/coffre4.png"));
			coffre4 = bufferedImg.getScaledInstance((int)(bufferedImg.getWidth()/ratio),(int)(bufferedImg.getHeight()/ratio), 0);
			bufferedImg = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("img/coffres/coffre5.png"));
			coffre5 = bufferedImg.getScaledInstance((int)(bufferedImg.getWidth()/ratio),(int)(bufferedImg.getHeight()/ratio), 0);
			bufferedImg = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("img/coffres/coffre6.png"));
			coffre6 = bufferedImg.getScaledInstance((int)(bufferedImg.getWidth()/ratio),(int)(bufferedImg.getHeight()/ratio), 0);
			bufferedImg = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("img/canon.png"));
			canon = bufferedImg.getScaledInstance((int)(bufferedImg.getWidth()/ratio),(int)(bufferedImg.getHeight()/ratio), 0);
			bufferedImg = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("img/pirate.png"));
			pirate = bufferedImg.getScaledInstance((int)(bufferedImg.getWidth()/ratio),(int)(bufferedImg.getHeight()/ratio), 0);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
